package com.林子祺.全名健康管理系统.view.servlet.login;

import com.林子祺.全名健康管理系统.controller.loginandregister.CheckRegistered;
import com.林子祺.全名健康管理系统.controller.loginandregister.CreateAccount;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//注册操作
@WebServlet("/register")
public class Register extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String registerName = req.getParameter("username");
        String registerPassword = req.getParameter("password");
        String registerEmail = req.getParameter("email");
        String registerGender = req.getParameter("gender");
        int age = Integer.parseInt(req.getParameter("age"));
        double height = Double.parseDouble(req.getParameter("height"));
        double weight = Double.parseDouble(req.getParameter("weight"));

            if (CheckRegistered.check(registerEmail) == true) {
                if (CreateAccount.create(registerName, registerPassword, registerEmail, registerGender, age, height, weight) == true)
                    resp.sendRedirect("/status/success.jsp");
                else
                    resp.sendRedirect("/status/fail.jsp");
            } else
                resp.sendRedirect("/status/fail.jsp");
        }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
